<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" type="text/css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" type="text/css">
    <link rel="stylesheet" href="./resources/prism/prism.css" type="text/css">
    <link rel="stylesheet" href="../css/ol.css" type="text/css">
    <link rel="stylesheet" href="./resources/layout.css" type="text/css">
    
    
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="./resources/zeroclipboard/ZeroClipboard.min.js"></script>
    <title>View Animation</title>
  </head>
  <body>

    <header class="navbar" role="navigation">
      <div class="container">
        <div class="display-table pull-left" id="navbar-logo-container">
          <a class="navbar-brand" href="./"><img src="./resources/logo-70x70.png">&nbsp;OpenLayers Examples</a>
        </div>
        <!-- menu items that get hidden below 768px width -->
        <nav class='collapse navbar-collapse navbar-responsive-collapse'>
          <ul class="nav navbar-nav pull-right">
            <li><a href="../doc">Docs</a></li>
            <li><a class="active" href="index.html">Examples</a></li>
            <li><a href="../apidoc">API</a></li>
            <li><a href="https://github.com/openlayers/openlayers">Code</a></li>
          </ul>
        </nav>
      </div>
    </header>

    <div class="container-fluid">

      <div id="latest-check" class="alert alert-warning alert-dismissible" role="alert" style="display:none">
        <button id="latest-dismiss" type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        This example uses OpenLayers v<span>4.6.5</span>. The <a id="latest-link" href="#" class="alert-link">latest</a> is v<span id="latest-version"></span>.
      </div>

      <div class="row-fluid">
        <div class="span12">
          <h4 id="title">View Animation</h4>
          <div id="map" class="map"></div>
<button id="rotate-left" title="Rotate clockwise">↻</button>
<button id="rotate-right" title="Rotate counterclockwise">↺</button>
<button id="pan-to-london">Pan to London</button>
<button id="elastic-to-moscow">Elastic to Moscow</button>
<button id="bounce-to-istanbul">Bounce to Istanbul</button>
<button id="spin-to-rome">Spin to Rome</button>
<button id="fly-to-bern">Fly to Bern</button>
<button id="rotate-around-rome">Rotate around Rome</button>
<button id="tour">Take a tour</button>

        </div>
      </div>

      <div class="row-fluid">
        <div class="span12">
          <p id="shortdesc">Demonstrates animated pan, zoom, and rotation.</p>
          <div id="docs"><p>This example shows how to use the <code>view.animate()</code> method to run one or more animations.</p>
</div>
          <div id="api-links">Related API documentation: <ul class="inline"><li><a href="../apidoc/ol.Map.html" title="API documentation for ol.Map">ol.Map</a></li>,<li><a href="../apidoc/ol.View.html" title="API documentation for ol.View">ol.View</a></li>,<li><a href="../apidoc/ol.easing.html" title="API documentation for ol.easing">ol.easing</a></li>,<li><a href="../apidoc/ol.layer.Tile.html" title="API documentation for ol.layer.Tile">ol.layer.Tile</a></li>,<li><a href="../apidoc/ol.proj.html" title="API documentation for ol.proj">ol.proj</a></li>,<li><a href="../apidoc/ol.source.OSM.html" title="API documentation for ol.source.OSM">ol.source.OSM</a></li></ul></div>
        </div>
      </div>

      <div class="row-fluid">
        <div id="source-controls">
          <a id="copy-button"><i class="fa fa-clipboard"></i> Copy</a>
          <a id="codepen-button"><i class="fa fa-codepen"></i> Edit</a>
        </div>
        <form method="POST" id="codepen-form" target="_blank" action="https://codepen.io/pen/define/">
          <textarea class="hidden" name="title">View Animation</textarea>
          <textarea class="hidden" name="description">Demonstrates animated pan, zoom, and rotation.</textarea>
          <textarea class="hidden" name="js">var london &#x3D; ol.proj.fromLonLat([-0.12755, 51.507222]);
var moscow &#x3D; ol.proj.fromLonLat([37.6178, 55.7517]);
var istanbul &#x3D; ol.proj.fromLonLat([28.9744, 41.0128]);
var rome &#x3D; ol.proj.fromLonLat([12.5, 41.9]);
var bern &#x3D; ol.proj.fromLonLat([7.4458, 46.95]);

var view &#x3D; new ol.View({
  center: istanbul,
  zoom: 6
});

var map &#x3D; new ol.Map({
  target: &#x27;map&#x27;,
  layers: [
    new ol.layer.Tile({
      preload: 4,
      source: new ol.source.OSM()
    })
  ],
  // Improve user experience by loading tiles while animating. Will make
  // animations stutter on mobile or slow devices.
  loadTilesWhileAnimating: true,
  view: view
});

// A bounce easing method (from https://github.com/DmitryBaranovskiy/raphael).
function bounce(t) {
  var s &#x3D; 7.5625, p &#x3D; 2.75, l;
  if (t &lt; (1 / p)) {
    l &#x3D; s * t * t;
  } else {
    if (t &lt; (2 / p)) {
      t -&#x3D; (1.5 / p);
      l &#x3D; s * t * t + 0.75;
    } else {
      if (t &lt; (2.5 / p)) {
        t -&#x3D; (2.25 / p);
        l &#x3D; s * t * t + 0.9375;
      } else {
        t -&#x3D; (2.625 / p);
        l &#x3D; s * t * t + 0.984375;
      }
    }
  }
  return l;
}

// An elastic easing method (from https://github.com/DmitryBaranovskiy/raphael).
function elastic(t) {
  return Math.pow(2, -10 * t) * Math.sin((t - 0.075) * (2 * Math.PI) / 0.3) + 1;
}

function onClick(id, callback) {
  document.getElementById(id).addEventListener(&#x27;click&#x27;, callback);
}

onClick(&#x27;rotate-left&#x27;, function() {
  view.animate({
    rotation: view.getRotation() + Math.PI / 2
  });
});

onClick(&#x27;rotate-right&#x27;, function() {
  view.animate({
    rotation: view.getRotation() - Math.PI / 2
  });
});

onClick(&#x27;rotate-around-rome&#x27;, function() {
  // Rotation animation takes the shortest arc, so animate in two parts
  var rotation &#x3D; view.getRotation();
  view.animate({
    rotation: rotation + Math.PI,
    anchor: rome,
    easing: ol.easing.easeIn
  }, {
    rotation: rotation + 2 * Math.PI,
    anchor: rome,
    easing: ol.easing.easeOut
  });
});

onClick(&#x27;pan-to-london&#x27;, function() {
  view.animate({
    center: london,
    duration: 2000
  });
});

onClick(&#x27;elastic-to-moscow&#x27;, function() {
  view.animate({
    center: moscow,
    duration: 2000,
    easing: elastic
  });
});

onClick(&#x27;bounce-to-istanbul&#x27;, function() {
  view.animate({
    center: istanbul,
    duration: 2000,
    easing: bounce
  });
});

onClick(&#x27;spin-to-rome&#x27;, function() {
  // Rotation animation takes the shortest arc, so animate in two parts
  var center &#x3D; view.getCenter();
  view.animate({
    center: [
      center[0] + (rome[0] - center[0]) / 2,
      center[1] + (rome[1] - center[1]) / 2
    ],
    rotation: Math.PI,
    easing: ol.easing.easeIn
  }, {
    center: rome,
    rotation: 2 * Math.PI,
    easing: ol.easing.easeOut
  });
});

function flyTo(location, done) {
  var duration &#x3D; 2000;
  var zoom &#x3D; view.getZoom();
  var parts &#x3D; 2;
  var called &#x3D; false;
  function callback(complete) {
    --parts;
    if (called) {
      return;
    }
    if (parts &#x3D;&#x3D;&#x3D; 0 || !complete) {
      called &#x3D; true;
      done(complete);
    }
  }
  view.animate({
    center: location,
    duration: duration
  }, callback);
  view.animate({
    zoom: zoom - 1,
    duration: duration / 2
  }, {
    zoom: zoom,
    duration: duration / 2
  }, callback);
}

onClick(&#x27;fly-to-bern&#x27;, function() {
  flyTo(bern, function() {});
});

function tour() {
  var locations &#x3D; [london, bern, rome, moscow, istanbul];
  var index &#x3D; -1;
  function next(more) {
    if (more) {
      ++index;
      if (index &lt; locations.length) {
        var delay &#x3D; index &#x3D;&#x3D;&#x3D; 0 ? 0 : 750;
        setTimeout(function() {
          flyTo(locations[index], next);
        }, delay);
      } else {
        alert(&#x27;Tour complete&#x27;);
      }
    } else {
      alert(&#x27;Tour cancelled&#x27;);
    }
  }
  next(true);
}

onClick(&#x27;tour&#x27;, tour);
</textarea>
          <textarea class="hidden" name="css"></textarea>
          <textarea class="hidden" name="html">&lt;div id&#x3D;&quot;map&quot; class&#x3D;&quot;map&quot;&gt;&lt;/div&gt;
&lt;button id&#x3D;&quot;rotate-left&quot; title&#x3D;&quot;Rotate clockwise&quot;&gt;↻&lt;/button&gt;
&lt;button id&#x3D;&quot;rotate-right&quot; title&#x3D;&quot;Rotate counterclockwise&quot;&gt;↺&lt;/button&gt;
&lt;button id&#x3D;&quot;pan-to-london&quot;&gt;Pan to London&lt;/button&gt;
&lt;button id&#x3D;&quot;elastic-to-moscow&quot;&gt;Elastic to Moscow&lt;/button&gt;
&lt;button id&#x3D;&quot;bounce-to-istanbul&quot;&gt;Bounce to Istanbul&lt;/button&gt;
&lt;button id&#x3D;&quot;spin-to-rome&quot;&gt;Spin to Rome&lt;/button&gt;
&lt;button id&#x3D;&quot;fly-to-bern&quot;&gt;Fly to Bern&lt;/button&gt;
&lt;button id&#x3D;&quot;rotate-around-rome&quot;&gt;Rotate around Rome&lt;/button&gt;
&lt;button id&#x3D;&quot;tour&quot;&gt;Take a tour&lt;/button&gt;
</textarea>
          <input type="hidden" name="resources" value="https://openlayers.org/en/v4.6.5/css/ol.css,https://openlayers.org/en/v4.6.5/build/ol.js">
          <input type="hidden" name="data">
        </form>
        <pre><code id="example-source" class="language-markup">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;View Animation&lt;/title&gt;
    &lt;link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css"&gt;
    &lt;!-- The line below is only needed for old environments like Internet Explorer and Android 4.x --&gt;
    &lt;script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"&gt;&lt;/script&gt;
    &lt;script src="https://openlayers.org/en/v4.6.5/build/ol.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id&#x3D;&quot;map&quot; class&#x3D;&quot;map&quot;&gt;&lt;/div&gt;
    &lt;button id&#x3D;&quot;rotate-left&quot; title&#x3D;&quot;Rotate clockwise&quot;&gt;↻&lt;/button&gt;
    &lt;button id&#x3D;&quot;rotate-right&quot; title&#x3D;&quot;Rotate counterclockwise&quot;&gt;↺&lt;/button&gt;
    &lt;button id&#x3D;&quot;pan-to-london&quot;&gt;Pan to London&lt;/button&gt;
    &lt;button id&#x3D;&quot;elastic-to-moscow&quot;&gt;Elastic to Moscow&lt;/button&gt;
    &lt;button id&#x3D;&quot;bounce-to-istanbul&quot;&gt;Bounce to Istanbul&lt;/button&gt;
    &lt;button id&#x3D;&quot;spin-to-rome&quot;&gt;Spin to Rome&lt;/button&gt;
    &lt;button id&#x3D;&quot;fly-to-bern&quot;&gt;Fly to Bern&lt;/button&gt;
    &lt;button id&#x3D;&quot;rotate-around-rome&quot;&gt;Rotate around Rome&lt;/button&gt;
    &lt;button id&#x3D;&quot;tour&quot;&gt;Take a tour&lt;/button&gt;
    &lt;script&gt;
      var london &#x3D; ol.proj.fromLonLat([-0.12755, 51.507222]);
      var moscow &#x3D; ol.proj.fromLonLat([37.6178, 55.7517]);
      var istanbul &#x3D; ol.proj.fromLonLat([28.9744, 41.0128]);
      var rome &#x3D; ol.proj.fromLonLat([12.5, 41.9]);
      var bern &#x3D; ol.proj.fromLonLat([7.4458, 46.95]);

      var view &#x3D; new ol.View({
        center: istanbul,
        zoom: 6
      });

      var map &#x3D; new ol.Map({
        target: &#x27;map&#x27;,
        layers: [
          new ol.layer.Tile({
            preload: 4,
            source: new ol.source.OSM()
          })
        ],
        // Improve user experience by loading tiles while animating. Will make
        // animations stutter on mobile or slow devices.
        loadTilesWhileAnimating: true,
        view: view
      });

      // A bounce easing method (from https://github.com/DmitryBaranovskiy/raphael).
      function bounce(t) {
        var s &#x3D; 7.5625, p &#x3D; 2.75, l;
        if (t &lt; (1 / p)) {
          l &#x3D; s * t * t;
        } else {
          if (t &lt; (2 / p)) {
            t -&#x3D; (1.5 / p);
            l &#x3D; s * t * t + 0.75;
          } else {
            if (t &lt; (2.5 / p)) {
              t -&#x3D; (2.25 / p);
              l &#x3D; s * t * t + 0.9375;
            } else {
              t -&#x3D; (2.625 / p);
              l &#x3D; s * t * t + 0.984375;
            }
          }
        }
        return l;
      }

      // An elastic easing method (from https://github.com/DmitryBaranovskiy/raphael).
      function elastic(t) {
        return Math.pow(2, -10 * t) * Math.sin((t - 0.075) * (2 * Math.PI) / 0.3) + 1;
      }

      function onClick(id, callback) {
        document.getElementById(id).addEventListener(&#x27;click&#x27;, callback);
      }

      onClick(&#x27;rotate-left&#x27;, function() {
        view.animate({
          rotation: view.getRotation() + Math.PI / 2
        });
      });

      onClick(&#x27;rotate-right&#x27;, function() {
        view.animate({
          rotation: view.getRotation() - Math.PI / 2
        });
      });

      onClick(&#x27;rotate-around-rome&#x27;, function() {
        // Rotation animation takes the shortest arc, so animate in two parts
        var rotation &#x3D; view.getRotation();
        view.animate({
          rotation: rotation + Math.PI,
          anchor: rome,
          easing: ol.easing.easeIn
        }, {
          rotation: rotation + 2 * Math.PI,
          anchor: rome,
          easing: ol.easing.easeOut
        });
      });

      onClick(&#x27;pan-to-london&#x27;, function() {
        view.animate({
          center: london,
          duration: 2000
        });
      });

      onClick(&#x27;elastic-to-moscow&#x27;, function() {
        view.animate({
          center: moscow,
          duration: 2000,
          easing: elastic
        });
      });

      onClick(&#x27;bounce-to-istanbul&#x27;, function() {
        view.animate({
          center: istanbul,
          duration: 2000,
          easing: bounce
        });
      });

      onClick(&#x27;spin-to-rome&#x27;, function() {
        // Rotation animation takes the shortest arc, so animate in two parts
        var center &#x3D; view.getCenter();
        view.animate({
          center: [
            center[0] + (rome[0] - center[0]) / 2,
            center[1] + (rome[1] - center[1]) / 2
          ],
          rotation: Math.PI,
          easing: ol.easing.easeIn
        }, {
          center: rome,
          rotation: 2 * Math.PI,
          easing: ol.easing.easeOut
        });
      });

      function flyTo(location, done) {
        var duration &#x3D; 2000;
        var zoom &#x3D; view.getZoom();
        var parts &#x3D; 2;
        var called &#x3D; false;
        function callback(complete) {
          --parts;
          if (called) {
            return;
          }
          if (parts &#x3D;&#x3D;&#x3D; 0 || !complete) {
            called &#x3D; true;
            done(complete);
          }
        }
        view.animate({
          center: location,
          duration: duration
        }, callback);
        view.animate({
          zoom: zoom - 1,
          duration: duration / 2
        }, {
          zoom: zoom,
          duration: duration / 2
        }, callback);
      }

      onClick(&#x27;fly-to-bern&#x27;, function() {
        flyTo(bern, function() {});
      });

      function tour() {
        var locations &#x3D; [london, bern, rome, moscow, istanbul];
        var index &#x3D; -1;
        function next(more) {
          if (more) {
            ++index;
            if (index &lt; locations.length) {
              var delay &#x3D; index &#x3D;&#x3D;&#x3D; 0 ? 0 : 750;
              setTimeout(function() {
                flyTo(locations[index], next);
              }, delay);
            } else {
              alert(&#x27;Tour complete&#x27;);
            }
          } else {
            alert(&#x27;Tour cancelled&#x27;);
          }
        }
        next(true);
      }

      onClick(&#x27;tour&#x27;, tour);
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>
      </div>
    </div>

    <script src="./resources/common.js"></script>
    <script src="./resources/prism/prism.min.js"></script>
    <script src="loader.js?id=animation"></script>
  </body>
  <script>
  var packageUrl = 'https://raw.githubusercontent.com/openlayers/openlayers.github.io/build/package.json';
  fetch(packageUrl).then(function(response) {
    return response.json();
  }).then(function(json) {
    var latestVersion = json.version;
    document.getElementById('latest-version').innerHTML = latestVersion;
    var url = window.location.href;
    var branchSearch = url.match(/\/([^\/]*)\/examples\//);
    var cookieText = 'dismissed=-' + latestVersion + '-';
    var dismissed = document.cookie.indexOf(cookieText) != -1;
    if (!dismissed && /^v[0-9\.]*$/.test(branchSearch[1]) && '4.6.5' != latestVersion) {
      var link = url.replace(branchSearch[0], '/latest/examples/');
      fetch(link, {method: 'head'}).then(function(response) {
        var a = document.getElementById('latest-link');
        a.href = response.status == 200 ? link : '../../latest/examples/';
      });
      var latestCheck = document.getElementById('latest-check');
      latestCheck.style.display = '';
      document.getElementById('latest-dismiss').onclick = function() {
        latestCheck.style.display = 'none';
        document.cookie = cookieText;
      }
    }
  });
  </script>
</html>
